/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.controller.hvac

import android.car.VehicleAreaSeat
import android.car.hardware.property.CarPropertyManager.CarPropertyEventCallback
import com.ecarx.systemui.plugin.model.hvac.HvacAutoFanSpeedImpl
import com.ecarx.systemui.plugin.model.hvac.HvacAutoOnImpl
import com.ecarx.systemui.plugin.model.hvac.HvacFanSpeedImpl

class HvacFanController private constructor() {

    private val mArea = VehicleAreaSeat.SEAT_ROW_1_LEFT or VehicleAreaSeat.SEAT_ROW_1_RIGHT

    private val manualFanSpeedImpl = HvacFanSpeedImpl()
    private val autoOnImpl = HvacAutoOnImpl()
    private val autoFanSpeedImpl = HvacAutoFanSpeedImpl()

    private val _defaultValueManual = 5
    private val _defaultValueAuto = 3 //auto

    suspend fun getFanSpeed(): Int {
        return if (isAutoOn()) {
            getAutoFanSpeed()
        } else {
            getManualFanSpeed()
        }
    }

    suspend fun registerManualFanSpeedCallback(callback: CarPropertyEventCallback) {
        if (!isManualFanSpeedAvailable()) {
            return
        }
        manualFanSpeedImpl.registerCallback(callback)
    }

    suspend fun unRegisterManualFanSpeedCallback(callback: CarPropertyEventCallback) {
        if (!isManualFanSpeedAvailable()) {
            return
        }
        manualFanSpeedImpl.unRegisterCallback(callback)
    }

    suspend fun registerAutoFanSpeedCallback(callback: CarPropertyEventCallback) {
        if (!isAutoFanSpeedAvailable()) {
            return
        }
        autoFanSpeedImpl.registerCallback(callback)
    }

    suspend fun unRegisterAutoFanSpeedCallback(callback: CarPropertyEventCallback) {
        if (!isAutoFanSpeedAvailable()) {
            return
        }
        autoFanSpeedImpl.unRegisterCallback(callback)
    }

    suspend fun registerAutoOnCallback(callback: CarPropertyEventCallback) {
        if (!isAutoOnAvailable()) {
            return
        }
        autoOnImpl.registerCallback(callback)
    }

    suspend fun unRegisterAutoOnCallback(callback: CarPropertyEventCallback) {
        if (!isAutoOnAvailable()) {
            return
        }
        autoOnImpl.unRegisterCallback(callback)
    }

    suspend fun isManualFanSpeedAvailable(): Boolean {
        return manualFanSpeedImpl.isAvailable(mArea)
    }

    suspend fun isAutoFanSpeedAvailable(): Boolean {
        return autoFanSpeedImpl.isAvailable(mArea)
    }

    suspend fun isAutoOnAvailable(): Boolean {
        return autoOnImpl.isAvailable(mArea)
    }

    suspend fun isAutoOn(): Boolean {
        if (isAutoOnAvailable()) {
            return autoOnImpl.getValue(mArea)
        }
        return false
    }

    suspend fun getManualFanSpeed(): Int {
        if (isManualFanSpeedAvailable()) {
            return manualFanSpeedImpl.getValue(mArea)
        }
        return _defaultValueManual
    }

    suspend fun getAutoFanSpeed(): Int {
        if (isAutoFanSpeedAvailable()) {
            return autoFanSpeedImpl.getValue(mArea)
        }
        return _defaultValueAuto
    }


    companion object {
        private const val TAG = "HvacFanController"

        fun getInstance() = Holder.instance
    }

    object Holder {
        val instance = HvacFanController()
    }
}